package unitroom

import (
	"gogogo-server/logicserver/gamelogic/unitrole"
	"gogogo-server/share/utils"
)

type RoomManager struct {
	allRooms map[uint64]*Room
}

var roomMgrInstance RoomManager

func RoomInstance() *RoomManager {
	return &roomMgrInstance
}

func (mgr *RoomManager) OnInit() {
	mgr.allRooms = make(map[uint64]*Room)
}

func (mgr *RoomManager) OnDestroy() {

}

func (mgr *RoomManager) CreateRoom(r *unitrole.Role) *Room {
	if rom := r.GetCurrentRoom(); rom != nil {
		return rom
	}
	// TODO 房间数量上限
	room := &Room{
		roles: make(map[uint64]bool),
	}
	room.RoomUid = uint64(utils.IDGenerator.GenerateRoomID())
	room.roles[r.RoleUid] = true
	mgr.allRooms[room.GetRoomUid()] = room
	return room
}

func (mgr *RoomManager) FindRoom(roomUid uint64) *Room {
	if r, ok := mgr.allRooms[roomUid]; ok {
		return r
	}
	return nil
}
